package com.bei.lingluan;


import com.bei.lingluan.generalDialog.GeneralButtonElement;
import com.bei.lingluan.generalDialog.GeneralDialogHelper;
import com.bei.lingluan.slice.InitializationAbilitySlice;
import com.bei.lingluan.slice.MainAbilitySlice;
import com.bei.lingluan.utils.Permissions;
import com.bei.lingluan.utils.PreferencesHelper;
import com.bei.lingluan.utils.ResourceHelper;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;

import java.util.Arrays;

/**
 * main ability
 *
 * @author Kirky
 */
public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(InitializationAbilitySlice.class.getName());
        addActionRoute("action.toSkeletonAnalyzer", MainAbilitySlice.class.getName());
        requestPermissions();
    }

    private void requestPermissions() {
        String[] permissions = {
//                SystemPermission.WRITE_MEDIA,
//                SystemPermission.READ_MEDIA,
                SystemPermission.CAMERA,
                SystemPermission.MICROPHONE,
                SystemPermission.LOCATION,
                SystemPermission.WRITE_USER_STORAGE,
                SystemPermission.READ_USER_STORAGE,

        };
        requestPermissionsFromUser(Arrays.stream(permissions)
                .filter(permission -> verifySelfPermission(permission) != IBundleManager.PERMISSION_GRANTED).toArray(String[]::new), 0);
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == Permissions.CAMERA.getResCode()) {
            if (grantResults.length > 0
                    && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
//                Permission granted
                /*Note: due to the time difference, whether there is permission in the interface
                 permission check, so the exception capture processing is carried out for those
                 interfaces that throw exceptions due to lack of permission*/
                PreferencesHelper preferencesHelper = new PreferencesHelper(getContext(), "permission");
                preferencesHelper.putSync(Permissions.CAMERA.getPerName(), true);
            } else {
//                 Permission denied
                new GeneralDialogHelper.Builder(getContext())
                        .setTitle(ResourceHelper.getString(getContext(), ResourceTable.String_failed_permission, "Failed to obtain permission", null))
                        .setContent(ResourceHelper.getString(getContext(), Permissions.CAMERA.getReason(), "CAMERA", null))
                        .addButton(ResourceHelper.getString(getContext(),ResourceTable.String_Delegating,"Delegating",null),
                                GeneralButtonElement.TYPE_ACCENT, null)
                        .addButton(ResourceHelper.getString(getContext(),ResourceTable.String_Cancel,"Cancel",null),
                                GeneralButtonElement.TYPE_NORMAL, null)
                        .create().show();
            }
        }
    }
}
